package com.ibiaoke.boss.controller;

import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.WeChatConsts;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.common.utils.*;
import com.ibiaoke.customer.consts.PayTypeConst;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.PtOpenGroup;
import com.ibiaoke.customer.entity.ThirdPartLogin;
import com.ibiaoke.customer.service.FightGroupService;
import com.ibiaoke.customer.service.OpenGroupService;
import com.ibiaoke.customer.service.ThirdPartLoginService;
import com.ibiaoke.goods.dao.PtGoodsDao;
import com.ibiaoke.goods.entity.PtGoods;
import com.ibiaoke.goods.service.PtGoodService;
import com.ibiaoke.order.dao.PtOrderDao;
import com.ibiaoke.order.entity.PtOrder;
import com.ibiaoke.order.entity.so.SubmitOrderSO;
import com.ibiaoke.order.service.PtOrderService;
import com.ibiaoke.order.service.PtPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * 开团Controller
 */
@Controller
public class WxPtOpenController extends BaseController {

    @Autowired
    private PtGoodService ptGoodService;

    @Autowired
    private PtPayOrderService ptPayOrderService;

    @Autowired
    private PtOrderService ptOrderService;

    @Autowired
    private OpenGroupService openGroupService;

    @Autowired
    private ThirdPartLoginService thirdPartLoginService;

    @Autowired
    private PtGoodsDao ptGoodsDao;
    /**
     * 一键开团生成订单
     */
    @RequestMapping("/submitPtGoodsOpenOrder")
    @ResponseBody
    public Res submitPtGoodsOrder(Integer ptGoodsId,Integer addressId,Integer deliveryType,Integer shopId){
        Customer customer = getLoginUser();
        AssertUtils.notNull(customer, "请登录");
        String openId = (String) getSession().getAttribute(WeChatConsts.OPEN_ID);

        ThirdPartLogin thirdPartLogin = thirdPartLoginService.getByUid(openId);

        PtOrder ptOrder = ptOrderService.submitPtGoodsOrder(customer,ptGoodsId,addressId,deliveryType,thirdPartLogin,shopId);
        return Res.create().addResponse("ptOrder",ptOrder);
    }

    @RequestMapping("/submitPtFlightOrder")
    @ResponseBody
    public Res submitPtGoodsOrder(SubmitOrderSO submitOrderSO){
        Customer customer = getLoginUser();

        AssertUtils.notNull(customer, "请登录");
        String openId = (String) getSession().getAttribute(WeChatConsts.OPEN_ID);

        ThirdPartLogin thirdPartLogin = thirdPartLoginService.getByUid(openId);

        PtOpenGroup openGroup = openGroupService.getPtOpenGroupByOpenCode(submitOrderSO.getKid());
        PtOrder ptOrder = ptOrderService.submitPtFlightOrder(submitOrderSO,openGroup,customer,thirdPartLogin);
        return Res.create().addResponse("ptOrder",ptOrder);
    }

    /**
     * 一键开团，付款
     */
    @RequestMapping("/payForPtGoods")
    @ResponseBody
    public Res payForPtGoodsOpen(Integer orderId,Integer PaymentType,Integer payType,Double payMoney, String smsCode, HttpServletRequest request) {
        //校验是否已成团
        ptOrderService.checkBeforPay(orderId);
        //更改PtOrder的金额和支付类型
        PtOrder ptOrder = ptOrderService.getPtOrderById(orderId);
        ptOrder.setPrice(new BigDecimal(payMoney));
        ptOrder.setPayType(payType);
        ptOrderService.UpdatePtOrder(ptOrder);
        //更改openGroup金额与支付类型
        openGroupService.updatePayTypeAndPayMoney(ptOrder.getKid(),payType,payMoney);
        
        String openId = (String) getSession().getAttribute(WeChatConsts.OPEN_ID);
        //微商城——微信支付
        if (PaymentType == PayTypeConst.WECAHT) {
            Map<String, String> payInfo = ptPayOrderService.payOrderByWeChat(orderId, openId);
            return Res.create().addResponse("payInfo", payInfo);
        }
        //微商城——支付宝支付
        if (PaymentType == PayTypeConst.ALIPAY) {
            String form = ptPayOrderService.payOrderByAliPay(orderId);
            return Res.create().addResponse("form", form);
        }
        //App微信支付
        if (PaymentType == PayTypeConst.APP_WECAHT) {
            String ip = IPUtil.getIp(request);
            Map<String, Object> payInfo = ptPayOrderService.payOrderByAppWeChat(orderId, ip);
            return Res.create().addResponse("payInfo", payInfo);
        }
        //App支付宝支付
        if (PaymentType == PayTypeConst.APP_ALIPAY) {
            String orderStr = ptPayOrderService.payOrderByAppAliPay(orderId);
            return Res.create().addResponse("orderStr", orderStr);
        }
        //预存款支付
        if (PaymentType == PayTypeConst.DEPOSIT) {
            ptPayOrderService.payOrderByDepositPay(orderId, smsCode);
            return Res.create();
        }
        return Res.create();
    }

    /**
     * 开团人列表
     * @param page
     * @return
     */
    @RequestMapping("/listOpenGroup")
    @ResponseBody
    public Res listOpenGroup(Integer ptGoodsId,Page page){
        List<PtOpenGroup> openGroupList = openGroupService.openGroupList(ptGoodsId,page);
        return Res.create().addResponse("openGroupList",openGroupList).addResponse("page",page);
    }

    /**
     * 一键开团，获取商品详情和开团人列表
     * @param ptGoodsId
     * @param page
     * @return
     */
    @RequestMapping("/goOpenGroup")
    @ResponseBody
    public Res goOpenGroup(Integer ptGoodsId,Page page){
        PtGoods ptGoods = ptGoodService.getGoodsById(ptGoodsId);
        List<PtOpenGroup> openGroupList = openGroupService.openGroupList(ptGoodsId,page);
        return Res.create().addResponse("ptGoods",ptGoods).addResponse("openGroupList",openGroupList).addResponse("page", page);
    }


    /**
     * 去参团获取商品详情和拼团人列表
     * @param
     * @return
     */
    @RequestMapping("/listFightGroup")
    @ResponseBody
    public Res listFightGroup(Integer ptGoodsId, Integer kid, String OpenCode){
        Customer customer = getLoginUser();
        PtOpenGroup ptOpenGroup = openGroupService.getPtOpenGroupByOpenCodeAndUserIdAndPtGoodsId(OpenCode,customer,ptGoodsId);
        Integer IsNotPT;
        if (ptOpenGroup != null &&  ptOpenGroup.getIsPay() == 1) {
            IsNotPT = 1;
        } else {
            IsNotPT = 0;
        }

        PtGoods ptGoods = ptGoodService.getGoodsById(ptGoodsId);
        List<PtOpenGroup> openGroupList = openGroupService.getPtOpenGroupByOpenCodeAndPtGoodsId(OpenCode, ptGoodsId);
        return Res.create()
                .addResponse("IsNotPT",IsNotPT)
                .addResponse("ptGoods",ptGoods)
                .addResponse("openGroupList",openGroupList);
    }

    //校验开团数量
    @RequestMapping("/checkPtCount")
    @ResponseBody
    public Res checkPtCount(Integer ptGoodsId) {
        PtGoods ptgood = ptGoodsDao.selectByPrimaryKey(ptGoodsId);
        if (ptgood.getPtCount() != null && ptgood.getPtCount() <= 0) {
            throw new ServiceException("此商品已售罄");
        }
        if(ptgood.getStatus() == 0){
            throw new ServiceException("此商品已下架");
        }
        return Res.create();
    }
}
